import os
import traceback
from playsound import playsound


def file_process(path, ext, path_out, ext_out, callback, header_filter = None, skip_exists = True):
    os.makedirs(path_out, exist_ok=True)

    rel_file_list = [relpath for relpath in os.listdir(path) if relpath.endswith(ext)]
    if header_filter is not None:
        rel_file_list = [relpath for relpath in rel_file_list if relpath[:6] in header_filter]

    in_file_list = [os.path.join(path, relpath) for relpath in rel_file_list]
    out_file_list = [os.path.join(path_out, relpath.replace(ext, ext_out)) for relpath in rel_file_list]
    n = 0
    try:
        for (in_file_path, out_file_path) in zip(in_file_list, out_file_list):
            if skip_exists and os.path.exists(out_file_path):
                n += 1
                continue

            callback(in_file_path, out_file_path)
            n += 1
    except Exception as e:
        print('Error:', e)
        traceback.print_exc()
    finally:
        print('%d / %d files handled by <%s>' % (n, len(out_file_list), callback.__name__))


def ring():
    playsound("z_ring.mp3")
